package com.xywy.fwc.net;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.util.Log;

public class Request {
	static ExecutorService  pool = Executors.newCachedThreadPool();
	
	public static void requestData(final BaseParser paser,
			final DataCallBack callable, final String urlStr) {
		Runnable t = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Log.e("url---", urlStr);
					URL url = new URL(urlStr);
					HttpURLConnection conn = (HttpURLConnection) url
							.openConnection();
					conn.setReadTimeout(15000);
					conn.setConnectTimeout(15000);
					int responseCode = conn.getResponseCode();
					if (responseCode == 200) {
						InputStream ins = conn.getInputStream();
						ByteArrayOutputStream bos = new ByteArrayOutputStream();
						int buff = 0;
						while ((buff = ins.read()) != -1) {
							bos.write(buff);
						}
						bos.flush();
						bos.close();
						String result = new String(bos.toByteArray());
						Object data = paser.parseJson(result);
						callable.dataCall(data);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					callable.dataCall(null);
				}

			}
		};
		pool.execute(t);
	}

	public static void postData(final BaseParser paser,
			final DataCallBack callable, final String urlStr,
			final HashMap<Object, Object> params) {
	 Runnable runnable = 	new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Log.e("url---", urlStr);
					URL url = new URL(urlStr);
					HttpURLConnection conn = (HttpURLConnection) url
							.openConnection();
					conn.setReadTimeout(15000);
					conn.setConnectTimeout(15000);
					conn.setDoInput(true);
					conn.setRequestMethod("POST");
					DataOutputStream out = new DataOutputStream(
							conn.getOutputStream());
					Set<java.util.Map.Entry<Object, Object>> set = params
							.entrySet();
					String param = "";
//					String param = "?";
					int i = 0;
					for (java.util.Map.Entry<Object, Object> entry : set) {
						if (i == 0)
							param += entry.getKey().toString() + "="
									+ entry.getValue();
						else
							param += "&" + entry.getKey().toString() + "="
									+ entry.getValue();

						i++;
					}
					out.writeBytes(param);
					out.flush();
					out.close();

					int responseCode = conn.getResponseCode();
					if (responseCode == 200) {
						InputStream ins = conn.getInputStream();
						ByteArrayOutputStream bos = new ByteArrayOutputStream();
						int buff = 0;
						while ((buff = ins.read()) != -1) {
							bos.write(buff);
						}
						bos.flush();
						bos.close();
						String result = new String(bos.toByteArray());
						System.out.println(result);
						Object data = paser.parseJson(result);
						callable.dataCall(data);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					callable.dataCall(null);
				}

			}
		};
		pool.execute(runnable);
	}
}
